﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FreedomERP.Dao.Bd;
using FreedomERP.Dao.Bd.Impl;
using FreedomERP.Models;
using FreedomERP.Models.Bd;
using FreedomERP.Comm;

namespace FreedomERP.Services.Bd.Impl
{
    /// <summary>
    /// 客户业务层实现类
    /// 作者：姚少鹏
    /// </summary>
    public class BdCustomerServiceImpl : IBdCustomerService
    {
        IBdCustomerDao customerDao = new BdCustomerDaoImpl();


        /// <summary>
        /// 根据FId获取客户
        /// </summary>
        /// <param name="FId"></param>
        /// <returns></returns>
        public BdCustomer getCustomerByFId(string FId)
        {
            return customerDao.getCustomerByFId(FId);
        }

        /// <summary>
        /// 根据FName获取客户
        /// </summary>
        /// <param name="FName"></param>
        /// <returns></returns>
        public BdCustomer getCustomerByFName(string FName)
        {
            return customerDao.getCustomerByFName(FName);
        }



        /// <summary>
        /// 查找有效的客户列表
        /// 作者：陈新杰
        /// </summary>
        /// <param name="param">查询参数</param>
        /// <param name="totalCount">数据库客户总数量，用于分页</param>
        /// <returns></returns>
        public List<BdCustomerView> findCustomerList(CustomerParameters param, out int totalCount)
        {
            List<BdCustomerView> result = new List<BdCustomerView>();
            IBdCategoryDao categoryDao = new BdCategoryDaoImpl();
            string validStatusId = categoryDao.QueryCategoryFIdByFTypeAndFNumber(ConstData.CATEGORY_CUSTOMER_STATUS_TYPE, "Y");
            param.fStatusID = validStatusId;
            DAOResult<BdCustomer> daoResult = customerDao.findCustomerList(param, out totalCount);
            if (daoResult.result == false)
            {
                throw new Exception(daoResult.msg);
            }
            foreach (var dbCustomer in daoResult.resultSet)
            {
                BdCustomerView customerView = new BdCustomerView();
                customerView.FId = dbCustomer.FId;
                customerView.FName = dbCustomer.FName;
                customerView.FNumber = dbCustomer.FNumber;
                customerView.FContactAddress = dbCustomer.FContactAddress;
                customerView.FContactEmail = dbCustomer.FContactEmail;
                customerView.FContactor = dbCustomer.FContactor;
                customerView.FContactPhone = dbCustomer.FContactPhone;
                customerView.FContactMobile = dbCustomer.FContactMobile;
                result.Add(customerView);
            }
            return result;
        }

        /// <summary>
        /// 新增或更新客户信息
        /// 作者：陈新杰
        /// </summary>
        /// <param name="customer"></param>
        /// <returns>客户的Fid</returns>
        public void saveOrUpdateCustomer(BdCustomerView customer, out String FId)
        {
            DAOResult<String> daoResult = customerDao.saveOrUpdateCustomer(customer);
            if (daoResult.result == false)
            {
                throw new Exception(daoResult.msg);
            }
            else
            {
          
                FId = daoResult.resultSet.First();
            }
        }


        /// <summary>
        /// 根据客户ID删除客户信息
        /// 作者：陈新杰
        /// </summary>
        /// <param name="FID"></param>
        /// <returns></returns>
        public void deleteCustomer(String FID)
        {
            DAOResult<String> daoResult = customerDao.deleteCustomer(FID);
            if (daoResult.result == false)
            {
                throw new Exception(daoResult.msg);
            }
        }
    }
}